#ifndef QQTUSERLISTCONTAINER_H
#define QQTUSERLISTCONTAINER_H

#include <QObject>
#include <QTimer>

#include <qqtudpclient.h>
#include <qqtudpserver.h>

#include <qqtprotocol.h>
#include <qqtprotocolmanager.h>
#include <qqtdataprotocol.h>
#include <qqtfindservermanager.h>

#include <qqtframe.h>

#include <qqthighgrade_global.h>

class QQtUserListProtocol;
/**
 * @brief The QQtUserListContainer class
 * 获取局域网在线用户列表
 *
 * 监听端口，必须是一致的，
 * 监听端口，就是接收端口，顺便作为发送端口。
 */
class QQTHIGHGRADESHARED_EXPORT QQtUserListContainer : public QObject
{
    Q_OBJECT

public:
    explicit QQtUserListContainer ( QObject* parent = 0 );
    virtual ~QQtUserListContainer();

    void setListeningAddress ( QHostAddress address, int port );
    void setListeningPort ( int port = 4410 );

    void setUserName ( QString username );
    void setUserContent ( QString key, QString value );

    QString getUserName();
    QString getUserContent ( QString key );

    int timerInterval();
    void setTimerInterval ( int millsecond );

signals:
    void signalUserInfoChanged ( const QQtDictionary& dict );

protected:
    //内部10s调用一次。
    void sendGetUserListRequest ( );

protected slots:
    void slotRecvUserInformation ( const QQtDictionary& data );
    void slotTimeout();

protected:

private:
    QQtDictionary mDictUser;

    QQtUdpClient* c0;
    QQtUserListProtocol* p0;

    QTimer* m_timer;
    QString mUUid;
};

/**
 * @brief The QQtUserListProtocol class
 * 局域网获取在线用户列表的协议。
 *
 * [0] "Who are you?"
 *
 * [0] "I am"
 * [1]  ["ip"] =
 *      ["port"] =
 *      ["uuid"] =
 *      ["user_name"] =
 *      ["user_content"] =
 *      ["heartbeats"] =
 */
class QQTHIGHGRADESHARED_EXPORT QQtUserListProtocol : public QQtDataProtocol
{
    Q_OBJECT
public:
    explicit QQtUserListProtocol ( QObject* parent = 0 );
    virtual ~QQtUserListProtocol();

    void setIPPort ( QString ip, int port );
    void setUUid ( QString uuid );
    void setUserName ( QString username );
    void setUserContent ( QString key, QString value );

    void sendWhoareyou();

    void recvWhoareyou();

    void sendIam();

    void recvIam ( const QQtDictionary& data );

signals:
    void signalRecvUserInformation ( const QQtDictionary& data );

private:
    QString mIP;
    int mPort;
    QString mUUid;
    QString mUserName;
    QQtDictionary mUserContent;

    // QQtDataProtocol interface
public:
    virtual void recvMessage ( const QQtDataSerialization& data ) override;
};

#endif // QQTUSERLISTCONTAINER_H

